package hash;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/*
 *@description
 *@author hkfan
 *@version 1.0
 *@create 2025/6/4 17:02
 */
public class LeetCode_219 {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        //0. 定义变量
        int l=0,r=k;
        Set<Integer> set = new HashSet<>();
        //1. 特殊情况
        if (k>=nums.length){
            r = nums.length-1;
        }
        //2. 加入set
        for (int i = 0; i <= r; i++) {
            if (set.contains(nums[i])){
                return true;
            }else {
                set.add(nums[i]);
            }
        }
        //3. 循环遍历
        while (r+1<nums.length){
            set.remove(nums[l]);
            l++;
            r++;
            if (set.contains(nums[r])){
                return true;
            }else {
                set.add(nums[r]);
            }
        }
        return false;
    }
}
